package com.leetcode.string;

import java.util.LinkedList;

/**
 * date:
 * description:
 */
public class RemoveDuplicates {
    public static String removeDuplicates(String str){
        LinkedList<Character> queue = new LinkedList<>();
        for(int i = 0; i < str.length(); i++){
            char ch = str.charAt(i);
            if(!queue.isEmpty() && queue.peekLast() == ch){
                queue.removeLast();
            }else{
                queue.addLast(ch);
            }
        }
        StringBuilder sb = new StringBuilder();
        int count = queue.size();
        for (int i = 0; i < count; i++){
            sb.append(queue.removeFirst());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "abbaca";
        String res = removeDuplicates(str);
        System.out.println(res);
    }
}
